<template>
  <div class="patient-reports-container">
    <!-- 头部区域 -->
    <div class="report-header">
      <h2><el-icon><Document /></el-icon> 我的医疗报告</h2>
      <div class="search-box">
        <el-input
            v-model="searchQuery"
            placeholder="搜索报告..."
            clearable
            @clear="handleSearchClear"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
      </div>
    </div>

    <!-- 报告列表 -->
    <div class="report-list">
      <el-card
          v-for="report in filteredReports"
          :key="report.id"
          class="report-card"
          @click="handlePreview(report)"
      >
        <div class="card-header">
          <span class="report-title">{{ report.title }}</span>
          <el-tag :type="getReportTypeTag(report.reportType)" size="small">
            {{ report.reportType }}
          </el-tag>
        </div>
        <div class="card-meta">
          <span><el-icon><User /></el-icon> {{ report.doctorName }}医生</span>
          <span><el-icon><Calendar /></el-icon> {{ formatDate(report.createTime) }}</span>
          <span><el-icon><CircleCheck /></el-icon> {{ report.status }}</span>
        </div>
      </el-card>

      <el-empty v-if="filteredReports.length === 0" description="暂无报告" />
    </div>

    <!-- 报告详情对话框 -->
    <el-dialog
        v-model="previewVisible"
        :title="currentReport.title"
        width="80%"
        top="5vh"
    >
      <div class="report-detail">
        <div class="detail-header">
          <div class="patient-info">
            <span><strong>患者姓名：</strong>{{ currentReport.patientName }}</span>
            <span><strong>报告日期：</strong>{{ formatDateTime(currentReport.createTime) }}</span>
          </div>
          <div class="doctor-info">
            <span><strong>主治医生：</strong>{{ currentReport.doctorName }}</span>
            <span><strong>报告类型：</strong>{{ currentReport.reportType }}</span>
          </div>
        </div>

        <div class="report-content">
          <h4>报告内容</h4>
          <pre>{{ currentReport.content }}</pre>
        </div>

        <div class="report-actions">
          <el-button type="primary" @click="handleDownload(currentReport)">
            <el-icon><Download /></el-icon> 下载报告
          </el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import {
  Document,
  Search,
  User,
  Calendar,
  CircleCheck,
  Download
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

// 数据状态
const reports = ref([])
const loading = ref(false)
const searchQuery = ref('')
const previewVisible = ref(false)
const currentReport = ref({
  id: '',
  title: '',
  patientName: '',
  doctorName: '',
  createTime: '',
  reportType: '',
  status: '',
  content: ''
})

// 模拟患者报告数据
const mockReports = [
  {
    id: 'R20230001',
    patientName: '张三',
    reportType: '检验报告',
    createTime: '2023-05-15 10:30:00',
    status: '已完成',
    title: '血常规检验报告',
    doctorName: '李医生',
    content: '白细胞计数: 5.6 ×10⁹/L (正常范围: 3.5-9.5)\n红细胞计数: 4.8 ×10¹²/L (正常范围: 4.3-5.8)\n血红蛋白: 135 g/L (正常范围: 130-175)\n血小板计数: 210 ×10⁹/L (正常范围: 125-350)\n\n检查结论: 血常规各项指标均在正常范围内。'
  },
  {
    id: 'R20230002',
    patientName: '张三',
    reportType: '影像报告',
    createTime: '2023-05-16 14:15:00',
    status: '已完成',
    title: '胸部X光检查报告',
    doctorName: '王医生',
    content: '检查所见:\n1. 双肺纹理清晰，未见明显实质性病变。\n2. 心影大小形态正常，心胸比率约0.48。\n3. 双侧膈面光滑，肋膈角锐利。\n4. 纵隔无增宽，气管居中。\n\n检查结论: 胸部X光检查未见明显异常。'
  },
  {
    id: 'R20230003',
    patientName: '张三',
    reportType: '体检报告',
    createTime: '2023-05-20 09:00:00',
    status: '已完成',
    title: '年度健康体检报告',
    doctorName: '赵医生',
    content: '体检结果:\n1. 血压: 120/80 mmHg\n2. 心率: 72次/分\n3. 空腹血糖: 5.2 mmol/L\n4. 肝功能: 正常\n5. 肾功能: 正常\n6. 血脂: 总胆固醇 4.8 mmol/L\n\n健康建议:\n1. 保持当前饮食和运动习惯\n2. 建议每年定期体检\n3. 注意保持规律作息'
  }
]

// 计算属性
const filteredReports = computed(() => {
  if (!searchQuery.value) return reports.value
  const query = searchQuery.value.toLowerCase()
  return reports.value.filter(report =>
      report.title.toLowerCase().includes(query) ||
      report.reportType.toLowerCase().includes(query) ||
      report.doctorName.toLowerCase().includes(query) ||
      report.createTime.toLowerCase().includes(query)
  )
})

// 生命周期钩子
onMounted(() => {
  fetchReports()
})

// 方法
function fetchReports() {
  loading.value = true
  // 模拟API请求
  setTimeout(() => {
    reports.value = mockReports
    loading.value = false
  }, 800)
}

function handlePreview(report) {
  currentReport.value = report
  previewVisible.value = true
}

function handleSearchClear() {
  searchQuery.value = ''
}

function handleDownload(report) {
  // 模拟下载功能
  ElMessage.success(`开始下载 ${report.title}`)
  // 实际项目中这里调用下载API
}

function getReportTypeTag(type) {
  const map = {
    '检验报告': 'primary',
    '影像报告': 'success',
    '体检报告': 'warning',
    '病理报告': 'danger'
  }
  return map[type] || 'info'
}

function formatDate(dateString) {
  return dateString.split(' ')[0]
}

function formatDateTime(dateString) {
  return dateString.replace(' ', '  ')
}
</script>

<style scoped>
.patient-reports-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.report-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 16px;
}

.search-box {
  width: 300px;
}

.report-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 16px;
}

.report-card {
  cursor: pointer;
  transition: all 0.3s;
  height: 100%;
}

.report-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.report-title {
  font-weight: bold;
  font-size: 16px;
  flex: 1;
  margin-right: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  color: #666;
  font-size: 13px;
}

.card-meta span {
  display: flex;
  align-items: center;
  gap: 4px;
}

.report-detail {
  padding: 0 10px;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 20px;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #eee;
}

.patient-info,
.doctor-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 200px;
}

.report-content {
  margin: 20px 0;
}

.report-content h4 {
  margin-bottom: 12px;
  color: #333;
}

.report-content pre {
  white-space: pre-wrap;
  font-family: inherit;
  line-height: 1.6;
  background: #f9f9f9;
  padding: 16px;
  border-radius: 4px;
  margin: 0;
}

.report-actions {
  display: flex;
  justify-content: center;
  margin-top: 24px;
}

@media (max-width: 768px) {
  .report-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .search-box {
    width: 100%;
  }

  .report-list {
    grid-template-columns: 1fr;
  }

  .detail-header {
    flex-direction: column;
  }
}
</style>
